<link type="text/css" href="<?php echo base_url();?>assets/js/development-bundle/themes/ui-lightness/jquery.ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="<?php echo base_url();?>assets/js/development-bundle/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/development-bundle/ui/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/development-bundle/ui/jquery.ui.widget.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/development-bundle/ui/jquery.ui.menu.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/development-bundle/ui/jquery.ui.button.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/development-bundle/ui/jquery.ui.position.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/development-bundle/ui/jquery.ui.autocomplete.js"></script>
<script language="javascript">
function back(){
	document.location="<?php echo site_url(); ?>/PenerimaanObat";
}
$(document).ready(function(){
	$(".tgl_terima").datepicker({
		changeMonth: true,
		changeYear: true,
		monthNamesShort: [ "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "Nopember", "Desember" ],
		dayNames: [ "Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu" ],
		dayNamesMin: [ "M", "S", "S", "R", "K", "J", "S" ],
		showOn: "button",
		dateFormat:"yy-mm-dd",
		buttonImage: "<?php echo base_url(); ?>assets/images/calendar.gif",
		buttonImageOnly: true
	});
});
(function( $ ) {
	$.widget( "ui.combobox", {
		_create: function() {
			var input,
				self = this,
				select = this.element.hide(),
				selected = select.children( ":selected" ),
				value = selected.val() ? selected.text() : "",
				wrapper = $( "<span>" )
					.addClass( "ui-combobox" )
					.insertAfter( select );

			input = $( "<input>" )
				.appendTo( wrapper )
				.val( value )
				.addClass( "ui-state-default" )
				.autocomplete({
					delay: 0,
					minLength: 0,
					source: function( request, response ) {
						var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
						response( select.children( "option" ).map(function() {
							var text = $( this ).text();
							if ( this.value && ( !request.term || matcher.test(text) ) )
								return {
									label: text.replace(
										new RegExp(
											"(?![^&;]+;)(?!<[^<>]*)(" +
											$.ui.autocomplete.escapeRegex(request.term) +
											")(?![^<>]*>)(?![^&;]+;)", "gi"
										), "<strong>$1</strong>" ),
									value: text,
									option: this
								};
						}) );
					},
					select: function( event, ui ) {
						ui.item.option.selected = true;
						self._trigger( "selected", event, {
							item: ui.item.option
						});
					},
					change: function( event, ui ) {
						if ( !ui.item ) {
							var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
								valid = false;
							select.children( "option" ).each(function() {
								if ( $( this ).text().match( matcher ) ) {
									this.selected = valid = true;
									return false;
								}
							});
							if ( !valid ) {
								// remove invalid value, as it didn't match anything
								$( this ).val( "" );
								select.val( "" );
								input.data( "autocomplete" ).term = "";
								return false;
							}
						}
					}
				})
				.addClass( "ui-widget ui-widget-content ui-corner-left" );

			input.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
				return $( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>" + item.label + "</a>" )
					.appendTo( ul );
			};

			$( "<a>" )
				.attr( "tabIndex", -1 )
				.attr( "title", "Show All Items" )
				.appendTo( wrapper )
				.button({
					icons: {
						primary: "ui-icon-triangle-1-s"
					},
					text: false
				})
				.removeClass( "ui-corner-all" )
				.addClass( "ui-corner-right ui-button-icon" )
				.click(function() {
					// close if already visible
					if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
						input.autocomplete( "close" );
						return;
					}

					// work around a bug (likely same cause as #5265)
					$( this ).blur();

					// pass empty string as value to search for, displaying all results
					input.autocomplete( "search", "" );
					input.focus();
				});
		},

		destroy: function() {
			this.wrapper.remove();
			this.element.show();
			$.Widget.prototype.destroy.call( this );
		}
	});
})( jQuery );

$(function() {
	$( "#nama_obat" ).combobox();
});
function gantiMerk(merk,satuan){
	var val	= merk.split("merk");
	var obat= document.formPenerimaanObat.elements["nama_obat"+val[1]].value;
	var id	= obat.split("-");
	<?php
	foreach($getObat as $row){
	?>
	if(id[0] == <?php echo $row["ID_OBAT"]?>){
		document.getElementById(merk).innerHTML="<?php echo $row["MEREK"]?>";
		document.getElementById(satuan).innerHTML="<?php echo $row["SATUAN"]?>";
	}
	<?php
	}
	?>
}
function clearObat(merk,satuan){
	var val	= merk.split("merk");
	document.formPenerimaanObat.elements["nama_obat"+val[1]].value = "";
	document.getElementById(merk).innerHTML="";
	document.getElementById(satuan).innerHTML="";
}
function addInput(divName){
	var	id		= document.getElementById("nama_obat").value;
	<?php
	foreach($getObat as $row){
	?>
		if(id == <?php echo $row["ID_OBAT"]; ?>){
			if(!document.formPenerimaanObat.elements["jumlah"+id]){
				var newdiv	= document.createElement("tr");
				newdiv.innerHTML = "<td><?php echo $row["NAMA_OBAT"]; ?></td><td><?php echo $row["MEREK"]; ?></td><td><input type=\"text\" class=\"input-mini\" name=\"jumlah"+id+"\" style=\"margin-bottom: 0px\"> <?php echo $row["SATUAN"]; ?></td><input class=\"btn btn-danger\" type=\"button\" value=\"-\" onClick=\"removeInput(this);\">";
				document.getElementById(divName).appendChild(newdiv);
				document.getElementById("jml_obat").value=id;
			}
		}
	<?
	}
	?>
}
function removeInput(parameter) {
	parameter.parentNode.parentNode.removeChild(parameter.parentNode);
}
function removeObat(obat){
	var obat	= document.getElementById(obat);
	var remove	= confirm("Anda yakin ingin menghapus data ini ?");
	if(remove == true){
		obat.parentNode.removeChild(obat);
	}
}
</script>
<style>
	.ui-combobox {
		position: relative;
		display: inline-block;
	}
	.ui-button {
		position: absolute;
		top: 0;
		bottom: 0;
		margin-left: -1px;
		padding: 0;
		/* adjust styles for IE 6/7 */
		*height: 1.7em;
		*top: 0.1em;
	}
	.ui-autocomplete-input {
		margin: 0;
		padding: 0.3em;
	}
	.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
		border: 1px solid #cccccc;
		background: #ffffff;
		font-weight: bold;
		color: #1c94c4;
	}
	.ui-widget {
		font-family:arial;
		font-size: 10pt;
		font-weight:normal;
		color:black;
	}
	.ui-datepicker-month{
		float:left;
	}
	.ui-datepicker-year{
		float:left;
	}
	.ui-datepicker-title{
		width:172px;
	}
</style>
<div class="form-head">Form Penerimaan Obat</div>
<hr>
<?php
echo form_open("/PenerimaanObat/formPenerimaanObat/".$parameter, array("name"=>"formPenerimaanObat","id"=>"medic_form"));
if($parameter == FALSE){
	$no_faktur	= set_value("no_faktur");
	$tgl_terima	= set_value("tgl_terima");
} else {
	$no_faktur	= $value["NO_FAKTUR"];
	$tgl_terima	= $value["TGL_PENERIMAAN"];
}
?>
<input type="hidden" name="jml_obat" id="jml_obat">
<p>
	<label>Nomor Faktur</label>
	<input type="text" name="no_faktur" value="<?php echo $no_faktur; ?>">
	<?php echo form_error("no_faktur"); ?>
</p>
<p>
	<label>Tanggal Penerimaan</label>
	<input type="text" name="tgl_terima" class="tgl_terima" value="<?php echo $tgl_terima; ?>" readonly>
	<?php echo form_error("tgl_terima"); ?>
</p>
<p style="width:300px;">
	<label>Nama Obat</label>
	<select name="nama_obat" id="nama_obat">
		<option value=""></option>
	<?php
	foreach($getObat as $row){
	?>
		<option value="<?php echo $row["ID_OBAT"]; ?>"><?php echo $row["NAMA_OBAT"]; ?></option>
	<?php
	}
	?>
	</select>
	<input style="float:right;" type="button" class="btn btn-success" value="+" onClick="addInput('detail_obat');">
	<?php echo form_error("nama_obat"); ?>
</p>
<table class="table table-bordered" id="detail_obat">
	<thead>
	<tr>
		<td>Nama Obat</td>
		<td>Merek</td>
		<td>Jumlah</td>
	</tr>
	</thead>
	<tbody>
	<?php
	if($parameter == TRUE){
		foreach($getDetail as $row){
	?>
	<tr id="dobat<?php echo $row["ID_OBAT"]; ?>">
		<td><?php echo $row["NAMA_OBAT"]; ?></td>
		<td><?php echo $row["MEREK"]; ?></td>
		<td><input type="text" class="input-mini" name="jumlah<?php echo $row["ID_OBAT"]; ?>" value="<?php echo $row["JUMLAH"]; ?>"> <?php echo $row["SATUAN"]; ?></td>
		<td style="border:none;"><input type="button" class="btn btn-danger" value="-" onClick="removeObat('dobat<?php echo $row["ID_OBAT"]; ?>');"></td>
	</tr>
	<?php
		}
	}
	?>
	</tbody>
</table>
<p>
	<input type="submit" class="btn btn-success" id="submit" value="Simpan">
	<input type="reset" class="btn btn-success" id="submit" value="Reset">
	<input type="button" class="btn btn-success" id="submit" value="Kembali" onClick="back()">
</p>
<?php echo form_close(); ?>